Option Strict Off
Option Explicit On
Imports System.Diagnostics.Process
Imports System.Threading
Imports System.Net.Mail

Public Class Connection
    Public WithEvents SBO_Application As SAPbouiCOM.Application
    Public ocompany As New SAPbobsCOM.Company
    Private oOrderForm As SAPbouiCOM.Form
    Public SboGuiApi As New SAPbouiCOM.SboGuiApi
    Public sConnectionString As String
    Private oNewItem As SAPbouiCOM.Item
    Private oFolderItem As SAPbouiCOM.Folder
    Dim oPetMaster As F_PetMaster
    Dim oAppointmentMaster As F_AppointmentMaster
    Dim oProgramMaster As F_ProgramMaster
    Dim oRouteMaster As F_RouteMaster
    Dim oActivities As F_Activities
    Dim oAttendance As F_Attendance
    Dim oGrouping As F_Grouping
    Dim oIncident As F_Incident
    Dim oFood_Medicine As F_Food_Medicine
    Dim oBoarding As F_Boarding
    Dim oF_Delete_RouteMaster As F_Delete_RouteMaster
    Dim oF_Training_Repors As F_Training_Repors
    Dim oR_Summary_Report As R_Summary_Report
    Dim oMenuEvent As MenuEvent
    Dim oF_Refund As F_Refund
    Dim oF_Recalculate As F_Recalculate
  
#Region "COnnce"

    '=======================
    Private Sub SetApplication()
        '*******************************************************************
        '// Use an SboGuiApi object to establish connection
        '// with the SAP Business One application and return an
        '// initialized appliction object
        '*******************************************************************
        Dim SboGuiApi As SAPbouiCOM.SboGuiApi
        Dim sConnectionString As String
        SboGuiApi = New SAPbouiCOM.SboGuiApi
        '// by following the steps specified above, the following
        '// Statment should be suficient for either development or run mode
        'sConnectionString = Environment.GetCommandLineArgs.GetValue(1)
        sConnectionString = Environment.GetCommandLineArgs.GetValue(1) '
        'sConnectionString = "5645523035496D706C656D656E746174696F6E3A59313931303035313531383699469FA92C3C9A964A219C5862952A90D911E9" 'Environment.GetCommandLineArgs.GetValue(1)'
        Try
            SboGuiApi.Connect(sConnectionString)
            '// connect to a running SBO Application
            '// get an initialized application object
            SBO_Application = SboGuiApi.GetApplication()
        Catch ex As Exception
            MsgBox("Make Sure That SAP Business One Application is running!!! ", MsgBoxStyle.Information)
            End
        End Try
        'SBO_Application.StatusBar.SetText("DI is Connecting now", SAPbouiCOM.BoMessageTime.bmt_Long, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
    End Sub
    ''=======================
    'Private Sub SetApplication()

    '    '*******************************************************************
    '    '// Use an SboGuiApi object to establish connection
    '    '// with the SAP Business One application and return an
    '    '// initialized appliction object
    '    '*******************************************************************

    '    Dim SboGuiApi As SAPbouiCOM.SboGuiApi
    '    Dim sConnectionString As String

    '    SboGuiApi = New SAPbouiCOM.SboGuiApi

    '    '// by following the steps specified above, the following
    '    '// statment should be suficient for either development or run mode

    '    sConnectionString = Environment.GetCommandLineArgs.GetValue(1)

    '    '// connect to a running SBO Application

    '    SboGuiApi.Connect(sConnectionString)

    '    '// get an initialized application object

    '    SBO_Application = SboGuiApi.GetApplication()

    'End Sub

    Private Function SetConnectionContext() As Integer

        Dim sCookie As String
        Dim sConnectionContext As String
        Dim lRetCode As Integer = 0

        '// First initialize the Company object

        oCompany = New SAPbobsCOM.Company

        '// Acquire the connection context cookie from the DI API.
        sCookie = oCompany.GetContextCookie

        '// Retrieve the connection context string from the UI API using the
        '// acquired cookie.
        sConnectionContext = SBO_Application.Company.GetConnectionContext(sCookie)

        '// before setting the SBO Login Context make sure the company is not
        '// connected

        If oCompany.Connected = True Then
            oCompany.Disconnect()
        End If

        '// Set the connection context information to the DI API.
        SetConnectionContext = oCompany.SetSboLoginContext(sConnectionContext)

    End Function

    Private Function ConnectToCompany() As Integer

        '// Establish the connection to the company database.
        ConnectToCompany = oCompany.Connect

    End Function
    'UPGRADE_NOTE: Class_Initialize was upgraded to Class_Initialize_Renamed. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1061"'
    Private Sub Class_Initialize_Renamed()

        '//*************************************************************
        '// set SBO_Application with an initialized application object
        '//*************************************************************

        SetApplication()

        '//*************************************************************
        '// Set The Connection Context
        '//*************************************************************

        If Not SetConnectionContext() = 0 Then
            SBO_Application.MessageBox("Failed setting a connection to DI API")
            End ' Terminating the Add-On Application
        End If


        '//*************************************************************
        '// Connect To The Company Data Base
        '//*************************************************************

        If Not ConnectToCompany() = 0 Then
            SBO_Application.MessageBox("Failed connecting to the company's Data Base")
            End ' Terminating the Add-On Application
        End If

        '//*************************************************************
        '// send an "hello world" message
        '//*************************************************************

        ' SBO_Application.MessageBox("DI Connected To: " & ocompany.CompanyName & vbNewLine & "Hello World!..Gopi")

    End Sub

#End Region
#Region "Connection"
    Public Sub New()
        MyBase.new()
        Try
            Class_Initialize_Renamed()
            LoadFromXML("Menu.xml", SBO_Application)
            Try
                oMenuItem = SBO_Application.Menus.Item("U1") 'moudles'
                oMenuItem.Image = "C:\k9_logo.jpg"
            Catch ex As Exception
                '  SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
            End Try
            oMenuEvent = New MenuEvent(ocompany, SBO_Application)
            oF_Recalculate = New F_Recalculate(ocompany, SBO_Application)
            oPetMaster = New F_PetMaster(ocompany, SBO_Application)
            oAppointmentMaster = New F_AppointmentMaster(ocompany, SBO_Application)
            oProgramMaster = New F_ProgramMaster(ocompany, SBO_Application)
            oRouteMaster = New F_RouteMaster(ocompany, SBO_Application)
            oActivities = New F_Activities(ocompany, SBO_Application)
            oAttendance = New F_Attendance(ocompany, SBO_Application)
            oGrouping = New F_Grouping(ocompany, SBO_Application)
            oIncident = New F_Incident(ocompany, SBO_Application)
            oFood_Medicine = New F_Food_Medicine(ocompany, SBO_Application)
            oBoarding = New F_Boarding(ocompany, SBO_Application)
            oF_Delete_RouteMaster = New F_Delete_RouteMaster(ocompany, SBO_Application)
            oF_Training_Repors = New F_Training_Repors(ocompany, SBO_Application)
            oR_Summary_Report = New R_Summary_Report(ocompany, SBO_Application)
            oF_Refund = New F_Refund(ocompany, SBO_Application)
            SBO_Application.MessageBox("Welcome To K9...")
            'DraftSO()
            'Lead()
            Try
                '   Auto_JV()
            Catch ex As Exception
                SBO_Application.MessageBox(ex.Message)
            End Try

        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Public Sub conn1()
        Dim sconn As String
        Dim ret As Integer
        Dim scook As String
        Dim str As String
        Try
            sconn = Environment.GetCommandLineArgs.GetValue(1)
            SboGuiApi.Connect(sconn)
            SBO_Application = SboGuiApi.GetApplication
            SboGuiApi = Nothing
            scook = ocompany.GetContextCookie
            str = SBO_Application.Company.GetConnectionContext(scook)
            ret = ocompany.SetSboLoginContext(str)
            ocompany.Connect()
            ocompany.GetLastError(ret, str)
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
   
#End Region
    Private Sub Auto_JV()
        oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        Dim dt As Date = Now.Date
        Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)

        Dim firstDayLastMonth As DateTime
        Dim lastDayLastMonth As DateTime
        Dim firstDayThisMonth As DateTime
        Dim sdt As String
        Dim tdt As String
        Dim sdt1 As String
        firstDayLastMonth = thisMonth.AddMonths(-1)
        firstDayThisMonth = thisMonth.AddMonths(0)
        lastDayLastMonth = thisMonth.AddDays(-1)
        sdt = Format(firstDayLastMonth.Date, "yyyyMMdd")
        sdt1 = Format(firstDayThisMonth.Date, "yyyyMMdd")
        Dim todaydt As String = Format(Now.Date, "dd")
        If todaydt <> "01" Then
            Exit Sub
        End If
        tdt = Format(lastDayLastMonth.Date, "yyyyMMdd")
        oRecordSet.DoQuery("SELECT count(*) as count,T0.[U_AI_PetCode], T0.[U_AI_PetName] FROM [dbo].[@AI_ATTENDANCE]  T0 WHERE T0.[U_AI_Date]  between '" & sdt & "' and '" & tdt & "' GROUP BY T0.[U_AI_PetCode], T0.[U_AI_PetName]")
        Dim M As Integer
        For M = 1 To oRecordSet.RecordCount

            Dim i As Integer
            Dim k As Integer
            Dim DayBal As Integer
            Dim count As Integer
            Dim newbal As Integer
            Dim up As Decimal
            Dim DocEntry As Integer

            Dim oPOR As SAPbobsCOM.Documents
            oPOR = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oPurchaseOrders)
            oPOR.GetByKey("")
            oPOR.Close()

            Dim oJV As SAPbobsCOM.JournalVouchers
            oJV = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalVouchers)
            count = oRecordSet.Fields.Item(0).Value
            oRecordSet5 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet4 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            If oRecordSet.RecordCount > 0 Then
                Dim petcode As String = oRecordSet.Fields.Item(1).Value
                Dim BPCode As String = OwnerCode(oRecordSet.Fields.Item(1).Value, ocompany)
                Dim PetSize As String = PSize(oRecordSet.Fields.Item(1).Value, ocompany)
                oRecordSet5.DoQuery("SELECT T0.[DocEntry], T0.[U_IvnoiceNo], T0.[U_CCode], T0.[U_UnitPrice], T0.[U_DayBal], T0.[U_Bought], T0.[U_PSize] FROM [dbo].[@AI_INVOICE_DAYPAAS]  T0 WHERE T0.[U_CCode] ='" & BPCode & "' and  T0.[U_PSize] ='" & PetSize & "' and T0.[U_DayBal] <> 0")
                If oRecordSet5.RecordCount > 0 Then
                    oRecordSet5.MoveFirst()
                    For i = 1 To oRecordSet5.RecordCount
                        DocEntry = oRecordSet5.Fields.Item(0).Value
                        DayBal = oRecordSet5.Fields.Item(4).Value
                        up = oRecordSet5.Fields.Item(3).Value
                        If count > DayBal Then
                            'Dim oJV As SAPbobsCOM.JournalVouchers
                            'oJV = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalVouchers)
                            Try
                                oJV.JournalEntries.Lines.AccountCode = "4-2810"
                                oJV.JournalEntries.Lines.Credit = (up * DayBal)
                                oJV.JournalEntries.Lines.Add()
                            Catch ex As Exception
                            End Try
                            Try
                                oJV.JournalEntries.Lines.AccountCode = "2-7100"
                                oJV.JournalEntries.Lines.Debit = (up * DayBal)
                                oJV.JournalEntries.Lines.Add()
                            Catch ex As Exception
                            End Try
                            Dim InvNo As String = oRecordSet5.Fields.Item(0).Value
                            oJV.JournalEntries.Memo = "Inv No " & InvNo
                            oJV.JournalEntries.Add()
                            ' k = oJV.Add()
                            count = count - DayBal
                            oRecordSet4.DoQuery("update [dbo].[@AI_INVOICE_DAYPAAS] set U_DayBal=0 where DocEntry=" & DocEntry & " ")
                        Else

                            Try
                                oJV.JournalEntries.Lines.AccountCode = "4-2810"
                                oJV.JournalEntries.Lines.Credit = (up * count)
                                oJV.JournalEntries.Lines.Add()
                            Catch ex As Exception
                            End Try
                            Try
                                oJV.JournalEntries.Lines.AccountCode = "2-7100"
                                oJV.JournalEntries.Lines.Debit = (up * count)
                                oJV.JournalEntries.Lines.Add()
                            Catch ex As Exception
                            End Try
                            Dim InvNo As String = oRecordSet5.Fields.Item(0).Value
                            oJV.JournalEntries.Memo = "Inv No " & InvNo
                            oJV.JournalEntries.Add()
                            ' k = oJV.Add()
                            newbal = DayBal - count
                            oRecordSet4.DoQuery("update [dbo].[@AI_INVOICE_DAYPAAS] set U_DayBal=" & newbal & " where DocEntry=" & DocEntry & " ")
                            Exit For
                        End If
                        oRecordSet5.MoveNext()
                    Next
                    k = oJV.Add()
                    Dim st As String = ""

                    ocompany.GetLastError(k, st)

                    st = st
                End If
            End If
            oRecordSet.MoveNext()
        Next
    End Sub
    Private Sub Invoice_DayPass(ByVal invno As String, ByVal ccode As String, ByVal UP As String, ByVal size As String, ByVal bal As Integer)
        Try
            Dim oGeneralService As SAPbobsCOM.GeneralService
            Dim oGeneralData As SAPbobsCOM.GeneralData
            Dim oSons As SAPbobsCOM.GeneralDataCollection
            Dim oSon As SAPbobsCOM.GeneralData
            Dim sCmp As SAPbobsCOM.CompanyService
            sCmp = ocompany.GetCompanyService
            oGeneralService = sCmp.GetGeneralService("AI_INVOICE_DAYPAAS")
            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
            oGeneralData.SetProperty("U_IvnoiceNo", invno)
            oGeneralData.SetProperty("U_CCode", ccode)
            oGeneralData.SetProperty("U_UnitPrice", UP)
            oGeneralData.SetProperty("U_PSize", size)
            oGeneralData.SetProperty("U_DayBal", bal)
            oGeneralData.SetProperty("U_Bought", bal)

            oGeneralService.Add(oGeneralData)
        Catch ex As Exception

        End Try
    End Sub
    Private Sub Invoice_AnnualPackage_update(ByVal invno As String, ByVal ccode As String, ByVal UP As String, ByVal size As String, ByVal bal As Integer)
        Try
            Dim oGeneralService As SAPbobsCOM.GeneralService
            Dim oGeneralData As SAPbobsCOM.GeneralData
            Dim oSons As SAPbobsCOM.GeneralDataCollection
            Dim oSon As SAPbobsCOM.GeneralData
            Dim sCmp As SAPbobsCOM.CompanyService
            sCmp = ocompany.GetCompanyService
            oGeneralService = sCmp.GetGeneralService("AI_INVOICE_DAYPAAS")
            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
            oGeneralData.SetProperty("U_IvnoiceNo", invno)
            oGeneralData.SetProperty("U_CCode", ccode)
            oGeneralData.SetProperty("U_UnitPrice", UP)
            oGeneralData.SetProperty("U_PSize", size)
            oGeneralData.SetProperty("U_DayBal", bal)
            oGeneralData.SetProperty("U_Bought", bal)

            oGeneralService.Add(oGeneralData)
        Catch ex As Exception

        End Try
    End Sub
    Private Sub SBO_Application_AppEvent(ByVal EventType As SAPbouiCOM.BoAppEventTypes) Handles SBO_Application.AppEvent
        If EventType = SAPbouiCOM.BoAppEventTypes.aet_CompanyChanged Then
            SBO_Application.StatusBar.SetText("Shuting Down K9 addon", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success)
            Windows.Forms.Application.Exit()
        End If

        If EventType = SAPbouiCOM.BoAppEventTypes.aet_ShutDown Then
            SBO_Application.StatusBar.SetText("Shuting Down K9 addon", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success)
            Windows.Forms.Application.Exit()
        End If

        If EventType = SAPbouiCOM.BoAppEventTypes.aet_ServerTerminition Then
            SBO_Application.StatusBar.SetText("Shuting Down K9 addon", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success)
            Windows.Forms.Application.Exit()
        End If
    End Sub
    # Region "CN"
    '************* CN **************
    Public Sub UpdateTransport_CN(ByVal DocNum As String)
        Try
            ' SBO_Application.StatusBar.SetText("Hi......", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
            oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

            oRecordSet.DoQuery("SELECT T0.[CardCode], T1.[Quantity] FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='110' and T0.DocEntry ='" & DocNum & "'")
        
            Dim CardCode As String = oRecordSet.Fields.Item(0).Value

            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim Used As Integer = 0
            Dim NewBalance As Integer = 0
            oRecordSet.MoveFirst()
            Dim Edt As String = Format(Now.Date.AddMonths(6), "yyyyMMdd")
            'T0.[U_AI_BoardExp]
            For i = 1 To oRecordSet.RecordCount
                Qty = oRecordSet.Fields.Item(1).Value
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoTrans], T0.[U_AI_AccumTrans],T0.[U_AI_TransUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Notrans - (Qty)
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = AccTrans - (Qty)
                Used = oRecordSet1.Fields.Item(2).Value
                NewBalance = NewNotrans - Used
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_TransBal=" & NewBalance & ",U_AI_NoTrans=" & NewNotrans & ",U_AI_AccumTrans=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            GC.Collect()

            oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3.DoQuery("SELECT T0.[CardCode], T1.[Quantity] FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='109' and T0.DocEntry ='" & DocNum & "'")
            CardCode = oRecordSet3.Fields.Item(0).Value
            oRecordSet3.MoveFirst()
            For i = 1 To oRecordSet3.RecordCount
                Qty = oRecordSet3.Fields.Item(1).Value
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoTrans], T0.[U_AI_AccumTrans],T0.[U_AI_TransUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Notrans - (Qty * 2)
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = AccTrans - (Qty * 2)
                Used = oRecordSet1.Fields.Item(2).Value
                NewBalance = NewNotrans - Used
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_TransBal=" & NewBalance & ",U_AI_NoTrans=" & NewNotrans & ",U_AI_AccumTrans=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet3.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            GC.Collect()

        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
        End Try
    End Sub

    Public Sub UpdateBoarding_CN(ByVal DocNum As String)
        Try
            ' SBO_Application.StatusBar.SetText("Hi....", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)

            oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[CardCode], T1.[Quantity] FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103' and T0.DocEntry ='" & DocNum & "'")
            If oRecordSet.RecordCount = 0 Then
                Exit Sub
            End If
            Dim Edt As String = Format(Now.Date.AddMonths(6), "yyyyMMdd")
            'T0.[U_AI_BoardExp]
            Dim CardCode As String = oRecordSet.Fields.Item(0).Value
            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim Used As Integer = 0
            Dim NewBalance As Integer = 0
            oRecordSet.MoveFirst()
            For i = 1 To oRecordSet.RecordCount
                Qty = oRecordSet.Fields.Item(1).Value
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_AccumBoard], T0.[U_AI_BoardUsed]  FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Notrans - Qty
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = AccTrans - Qty
                Used = oRecordSet1.Fields.Item(2).Value
                NewBalance = NewNotrans - Used
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_BoardBal='" & NewBalance & "',U_AI_BoardExp='" & Edt & "',U_AI_NoBoard=" & NewNotrans & ",U_AI_AccumBoard=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            GC.Collect()
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
        End Try
    End Sub
    Public Sub UpdateDayPass_CN(ByVal DocNum As String)
        Try
            'SBO_Application.StatusBar.SetText("Hi............", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
            oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet5 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet51 As SAPbobsCOM.Recordset
            oRecordSet51 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet5.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[Price],T1.[U_AI_PetCode] FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='104') and T0.DocEntry ='" & DocNum & "'")
            oRecordSet51.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[Price],T1.[U_AI_PetCode] FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102') and T0.DocEntry ='" & DocNum & "'")

            Dim PetSize As String = ""
            oRecordSet.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[U_AI_PetCode],T1.[LineNum] FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='104') and T0.DocEntry ='" & DocNum & "'")

            Dim oRecordSet6 As SAPbobsCOM.Recordset
            oRecordSet6 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet6.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[U_AI_PetCode],T1.[U_AI_PackageType] FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102') and T0.DocEntry ='" & DocNum & "'")
            If oRecordSet.RecordCount = 0 And oRecordSet6.RecordCount = 0 Then
                Exit Sub
            End If

            'If oRecordSet51.RecordCount > 0 Then
            '    PetSize = PSize(oRecordSet51.Fields.Item(3).Value, ocompany)
            '    Dim CNo As String = oRecordSet51.Fields.Item(0).Value.ToString
            '    Dim upp As String = oRecordSet51.Fields.Item("Price").Value
            '    Dim dpbou As String = oRecordSet51.Fields.Item(1).Value
            '    Invoice_DayPass(DocNum, CNo, upp, PetSize, dpbou)
            '    ' Invoice_AnnualPackage_update(DocNum, CNo, upp, PetSize, dpbou)
            'End If

            'If oRecordSet5.RecordCount > 0 Then
            '    PetSize = PSize(oRecordSet5.Fields.Item(3).Value, ocompany)
            '    Dim CNo As String = oRecordSet5.Fields.Item(0).Value.ToString
            '    Dim upp As String = oRecordSet5.Fields.Item("Price").Value
            '    Dim dpbou As String = oRecordSet5.Fields.Item(1).Value
            '    Invoice_DayPass(DocNum, CNo, upp, PetSize, dpbou)
            '    ' Invoice_AnnualPackage_update(DocNum, CNo, upp, PetSize, dpbou)
            'End If


            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim SmallCount As Integer = 0
            Dim MediumCount As Integer = 0
            Dim LargeCount As Integer = 0
            oRecordSet.MoveFirst()
            Dim Edt As String = Format(Now.Date.AddMonths(6), "yyyyMMdd")
            '[U_AI_ExpDate]
            Dim Small As Boolean = False
            Dim Medium As Boolean = False
            Dim Large As Boolean = False
            Dim Qty1 As Integer = 0
            For i = 1 To oRecordSet.RecordCount
                Dim CardCode As String = oRecordSet.Fields.Item(0).Value
                PetSize = PSize(oRecordSet.Fields.Item(2).Value, ocompany)
                Qty1 = Qty1 + oRecordSet.Fields.Item(1).Value
                If PetSize = "Small" Then
                    Small = True
                    SmallCount = SmallCount + 1
                ElseIf PetSize = "Medium" Then
                    Medium = True
                    MediumCount = MediumCount + 1
                ElseIf PetSize = "Large" Then
                    Large = True
                    LargeCount = LargeCount + 1
                End If
                oRecordSet.MoveNext()
            Next
            oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0 order by T0.[U_AI_DayPassBonus]")
            Dim j As Integer
            oRecordSet3.MoveFirst()
            Dim Bonus1 As Integer = 0
            Dim Bought1 As Integer = 0
            Dim BonusPoint1 As Integer = 0
            For j = 1 To oRecordSet3.RecordCount
                Bought1 = oRecordSet3.Fields.Item(0).Value
                Bonus1 = oRecordSet3.Fields.Item(1).Value
                If Qty1 >= Bought1 Then
                    BonusPoint1 = Bonus1
                End If
                oRecordSet3.MoveNext()
            Next
            Dim SmallBP As Integer = 0
            Dim MediumBP As Integer = 0
            Dim LargeBP As Integer = 0
            Dim BonusPoint2 As Integer = 0
            BonusPoint2 = BonusPoint1
            For j = 1 To BonusPoint1
                If Small = True Then
                    SmallBP = SmallBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
                If Medium = True Then
                    MediumBP = MediumBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
                If Large = True Then
                    LargeBP = LargeBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
            Next
            Dim LineNum As Integer = 0
            oRecordSet.MoveFirst()
            For i = 1 To oRecordSet.RecordCount
                Dim CardCode As String = oRecordSet.Fields.Item(0).Value

                PetSize = PSize(oRecordSet.Fields.Item(2).Value, ocompany)
                LineNum = oRecordSet.Fields.Item(3).Value
                If PetSize = "Small" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = (Notrans - (SmallBP)) - Qty
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = AccTrans - Qty
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'update ocrd set groupcode=" & GroupCode & ",
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                    Try
                        oRecordSet2.DoQuery("UPDATE INV1 set U_FreeDP=" & (SmallBP) & " where [DocEntry]=" & DocNum & " and LineNum=" & LineNum & "")
                    Catch ex As Exception

                    End Try
                    If SmallBP <> 0 Then
                        SmallBP = 0
                    End If
                ElseIf PetSize = "Medium" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassM], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedM] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    'Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = (Notrans - (MediumBP)) - Qty
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = AccTrans - Qty
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "', U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "', U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                    Try
                        oRecordSet2.DoQuery("UPDATE INV1 set U_FreeDP=" & (MediumBP) & " where [DocEntry]=" & DocNum & " and LineNum=" & LineNum & "")
                    Catch ex As Exception

                    End Try
                    If MediumBP <> 0 Then
                        MediumBP = 0
                    End If
                ElseIf PetSize = "Large" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassB], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedB] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    'Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = (Notrans - (LargeBP)) - Qty
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = AccTrans - Qty
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "' , groupcode=" & GroupCode & ",U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                    Try
                        oRecordSet2.DoQuery("UPDATE INV1 set U_FreeDP=" & (LargeBP) & " where [DocEntry]=" & DocNum & " and LineNum=" & LineNum & "")
                    Catch ex As Exception

                    End Try
                    If LargeBP <> 0 Then
                        LargeBP = 0
                    End If
                End If

                oRecordSet.MoveNext()
            Next
            '---annual pack
            Edt = Format(Now.Date.AddMonths(12), "yyyyMMdd")
            For i = 1 To oRecordSet6.RecordCount
                Dim CardCode As String = oRecordSet6.Fields.Item(0).Value

                PetSize = PSize(oRecordSet6.Fields.Item(2).Value, ocompany)
                Dim PackType = oRecordSet6.Fields.Item(3).Value
                Dim oRecordSet7 As SAPbobsCOM.Recordset
                oRecordSet7 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet7.DoQuery("SELECT T0.[U_AI_DaysPerWeek] FROM [dbo].[@AI_CONTRACTTYPE]  T0 WHERE T0.[Code] ='" & PackType & "'")
                If PetSize = "Small" Then
                    Qty = oRecordSet6.Fields.Item(1).Value
                    If oRecordSet7.Fields.Item(0).Value = 7 Then
                        Dim year As Integer = Now.Date.Year
                        Dim Month As Integer = Now.Date.Month
                        Dim days As Integer = System.DateTime.DaysInMonth(year, Month)
                        Qty = Qty * days
                    Else
                        Qty = Qty * oRecordSet7.Fields.Item(0).Value * 4
                    End If
                    'oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Notrans + BonusPoint - Qty
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = AccTrans + BonusPoint - Qty
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewAccTrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'update ocrd set groupcode=" & GroupCode & ",
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                ElseIf PetSize = "Medium" Then

                    Qty = oRecordSet6.Fields.Item(1).Value
                    If oRecordSet7.Fields.Item(0).Value = 7 Then
                        Dim year As Integer = Now.Date.Year
                        Dim Month As Integer = Now.Date.Month
                        Dim days As Integer = System.DateTime.DaysInMonth(year, Month)
                        Qty = Qty * days
                    Else
                        Qty = Qty * oRecordSet7.Fields.Item(0).Value * 4
                    End If
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassM], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedM] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    'oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Notrans + BonusPoint - Qty
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = AccTrans + BonusPoint - Qty
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewAccTrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                ElseIf PetSize = "Large" Then
                    Qty = oRecordSet6.Fields.Item(1).Value
                    If oRecordSet7.Fields.Item(0).Value = 7 Then
                        Dim year As Integer = Now.Date.Year
                        Dim Month As Integer = Now.Date.Month
                        Dim days As Integer = System.DateTime.DaysInMonth(year, Month)
                        Qty = Qty * days
                    Else
                        Qty = Qty * oRecordSet7.Fields.Item(0).Value * 4
                    End If
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassB], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedB] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Notrans + BonusPoint - Qty
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = AccTrans + BonusPoint - Qty
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewAccTrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "' , groupcode=" & GroupCode & ",U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                End If

                oRecordSet6.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            oRecordSet6 = Nothing
            GC.Collect()
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
        End Try
    End Sub

    '************ End CN *****************
#End Region
#Region "Invoice"


    Public Sub UpdateTransport(ByVal DocNum As String)
        Try

            oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
           
            oRecordSet.DoQuery("SELECT T0.[CardCode], T1.[Quantity] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='110' and T0.DocEntry ='" & DocNum & "'")
            'If oRecordSet.RecordCount = 0 Then
            '    Exit Sub
            'End If
            Dim CardCode As String = oRecordSet.Fields.Item(0).Value
            'Dim orecordset_count As SAPbobsCOM.Recordset
            'orecordset_count = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            'orecordset_count.DoQuery("SELECT count(*) as 'total' FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_AI_CustCode]  ='" & CardCode & "' and T0.[U_AI_Active]='Y' GROUP BY T0.[U_AI_CustCode]")
            'Dim count As Integer = orecordset_count.Fields.Item(0).Value


            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim Used As Integer = 0
            Dim NewBalance As Integer = 0
            oRecordSet.MoveFirst()
            Dim Edt As String = Format(Now.Date.AddMonths(6), "yyyyMMdd")
            'T0.[U_AI_BoardExp]
            For i = 1 To oRecordSet.RecordCount
                Qty = oRecordSet.Fields.Item(1).Value
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoTrans], T0.[U_AI_AccumTrans],T0.[U_AI_TransUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Notrans + (Qty)
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = AccTrans + (Qty)
                Used = oRecordSet1.Fields.Item(2).Value
                NewBalance = NewNotrans - Used
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_TransBal=" & NewBalance & ",U_AI_NoTrans=" & NewNotrans & ",U_AI_AccumTrans=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            GC.Collect()

            oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3.DoQuery("SELECT T0.[CardCode], T1.[Quantity] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='109' and T0.DocEntry ='" & DocNum & "'")
            CardCode = oRecordSet3.Fields.Item(0).Value
            'orecordset_count = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            'orecordset_count.DoQuery("SELECT count(*) as 'total' FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_AI_CustCode]  ='" & CardCode & "' and T0.[U_AI_Active]='Y' GROUP BY T0.[U_AI_CustCode]")
            'count = orecordset_count.Fields.Item(0).Value
            oRecordSet3.MoveFirst()
            For i = 1 To oRecordSet3.RecordCount
                Qty = oRecordSet3.Fields.Item(1).Value
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoTrans], T0.[U_AI_AccumTrans],T0.[U_AI_TransUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Notrans + (Qty * 2)
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = AccTrans + (Qty * 2)
                Used = oRecordSet1.Fields.Item(2).Value
                NewBalance = NewNotrans - Used
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_TransBal=" & NewBalance & ",U_AI_NoTrans=" & NewNotrans & ",U_AI_AccumTrans=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet3.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            GC.Collect()

        Catch ex As Exception

        End Try
    End Sub
    Public Sub UpdateBoarding(ByVal DocNum As String)
        Try
            oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[CardCode], T1.[Quantity],DateAdd(mm,6,T0.[DocDate]) 'DocDate' FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103' and T0.DocEntry ='" & DocNum & "'")
            If oRecordSet.RecordCount = 0 Then
                Exit Sub
            End If
            Dim Edt As String = Format(oRecordSet.Fields.Item("DocDate").Value, "yyyyMMdd")
            'T0.[U_AI_BoardExp]
            Dim CardCode As String = oRecordSet.Fields.Item(0).Value
            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim Used As Integer = 0
            Dim NewBalance As Integer = 0
            oRecordSet.MoveFirst()
            For i = 1 To oRecordSet.RecordCount
                Qty = oRecordSet.Fields.Item(1).Value
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_AccumBoard], T0.[U_AI_BoardUsed]  FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Qty + Notrans
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = Qty + AccTrans
                Used = oRecordSet1.Fields.Item(2).Value
                NewBalance = NewNotrans - Used
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_BoardBal='" & NewBalance & "',U_AI_BoardExp='" & Edt & "',U_AI_NoBoard=" & NewNotrans & ",U_AI_AccumBoard=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            GC.Collect()
        Catch ex As Exception
        End Try
    End Sub
    Public Sub UpdateDayPass(ByVal DocNum As String)
        Try
            oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet5 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet51 As SAPbobsCOM.Recordset
            oRecordSet51 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet5.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[Price],T1.[U_AI_PetCode],DateAdd(mm,6,T0.[DocDate]) 'DocDate' FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='104') and T0.DocEntry ='" & DocNum & "'")
            oRecordSet51.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[Price],T1.[U_AI_PetCode],DateAdd(mm,6,T0.[DocDate]) 'DocDate' FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102') and T0.DocEntry ='" & DocNum & "'")

            Dim PetSize As String = ""
            oRecordSet.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[U_AI_PetCode],T1.[LineNum],DateAdd(mm,6,T0.[DocDate]) 'DocDate' FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='104') and T0.DocEntry ='" & DocNum & "'")
           
            Dim oRecordSet6 As SAPbobsCOM.Recordset
            oRecordSet6 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet6.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[U_AI_PetCode],T1.[U_AI_PackageType] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102') and T0.DocEntry ='" & DocNum & "'")
            If oRecordSet.RecordCount = 0 And oRecordSet6.RecordCount = 0 Then
                Exit Sub
            End If
            Dim Edt As String = Format(Now.Date.AddMonths(6), "yyyyMMdd")
            If oRecordSet51.RecordCount > 0 Then
                Edt = Format(oRecordSet51.Fields.Item("DocDate").Value, "yyyyMMdd")
                PetSize = PSize(oRecordSet51.Fields.Item(3).Value, ocompany)
                Dim CNo As String = oRecordSet51.Fields.Item(0).Value.ToString
                Dim upp As String = oRecordSet51.Fields.Item("Price").Value
                Dim dpbou As String = oRecordSet51.Fields.Item(1).Value
                Invoice_DayPass(DocNum, CNo, upp, PetSize, dpbou)
                ' Invoice_AnnualPackage_update(DocNum, CNo, upp, PetSize, dpbou)
            End If

            If oRecordSet5.RecordCount > 0 Then
                Edt = Format(oRecordSet5.Fields.Item("DocDate").Value, "yyyyMMdd")
                PetSize = PSize(oRecordSet5.Fields.Item(3).Value, ocompany)
                Dim CNo As String = oRecordSet5.Fields.Item(0).Value.ToString
                Dim upp As String = oRecordSet5.Fields.Item("Price").Value
                Dim dpbou As String = oRecordSet5.Fields.Item(1).Value
                Invoice_DayPass(DocNum, CNo, upp, PetSize, dpbou)
                ' Invoice_AnnualPackage_update(DocNum, CNo, upp, PetSize, dpbou)
            End If
         
            
            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim SmallCount As Integer = 0
            Dim MediumCount As Integer = 0
            Dim LargeCount As Integer = 0
            If oRecordSet.RecordCount > 0 Then
                oRecordSet.MoveFirst()
            End If




            '[U_AI_ExpDate]
            Dim Small As Boolean = False
            Dim Medium As Boolean = False
            Dim Large As Boolean = False
            Dim Qty1 As Integer = 0
            For i = 1 To oRecordSet.RecordCount
                Dim CardCode As String = oRecordSet.Fields.Item(0).Value
                PetSize = PSize(oRecordSet.Fields.Item(2).Value, ocompany)
                Qty1 = Qty1 + oRecordSet.Fields.Item(1).Value
                If PetSize = "Small" Then
                    Small = True
                    SmallCount = SmallCount + 1
                ElseIf PetSize = "Medium" Then
                    Medium = True
                    MediumCount = MediumCount + 1
                ElseIf PetSize = "Large" Then
                    Large = True
                    LargeCount = LargeCount + 1
                End If
                oRecordSet.MoveNext()
            Next
            oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0 order by T0.[U_AI_DayPassBonus]")
            Dim j As Integer
            oRecordSet3.MoveFirst()
            Dim Bonus1 As Integer = 0
            Dim Bought1 As Integer = 0
            Dim BonusPoint1 As Integer = 0
            For j = 1 To oRecordSet3.RecordCount
                Bought1 = oRecordSet3.Fields.Item(0).Value
                Bonus1 = oRecordSet3.Fields.Item(1).Value
                If Qty1 >= Bought1 Then
                    BonusPoint1 = Bonus1
                End If
                oRecordSet3.MoveNext()
            Next
            Dim SmallBP As Integer = 0
            Dim MediumBP As Integer = 0
            Dim LargeBP As Integer = 0
            Dim BonusPoint2 As Integer = 0
            BonusPoint2 = BonusPoint1
            For j = 1 To BonusPoint1
                If Small = True Then
                    SmallBP = SmallBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
                If Medium = True Then
                    MediumBP = MediumBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
                If Large = True Then
                    LargeBP = LargeBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
            Next
            Dim LineNum As Integer = 0
            oRecordSet.MoveFirst()
            For i = 1 To oRecordSet.RecordCount
                ' Edt = Format(oRecordSet.Fields.Item("DocDate").Value, "yyyyMMdd")
                Dim CardCode As String = oRecordSet.Fields.Item(0).Value

                PetSize = PSize(oRecordSet.Fields.Item(2).Value, ocompany)
                LineNum = oRecordSet.Fields.Item(3).Value
                If PetSize = "Small" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + (SmallBP)
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans '+ (SmallBP)
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused

                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'update ocrd set groupcode=" & GroupCode & ",
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If
                    Try
                        oRecordSet2.DoQuery("UPDATE INV1 set U_FreeDP=" & (SmallBP) & " where [DocEntry]=" & DocNum & " and LineNum=" & LineNum & "")
                    Catch ex As Exception

                    End Try
                    If SmallBP <> 0 Then
                        SmallBP = 0
                    End If
                ElseIf PetSize = "Medium" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassM], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedM] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    'Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + (MediumBP)
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans '+ (MediumBP)
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused

                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "', U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "', U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If
                    Try
                        oRecordSet2.DoQuery("UPDATE INV1 set U_FreeDP=" & (MediumBP) & " where [DocEntry]=" & DocNum & " and LineNum=" & LineNum & "")
                    Catch ex As Exception

                    End Try

                    If MediumBP <> 0 Then
                        MediumBP = 0
                    End If
                ElseIf PetSize = "Large" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassB], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedB] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    'Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + (LargeBP)
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans '+ (LargeBP)
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "' , groupcode=" & GroupCode & ",U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If
                    Try
                        oRecordSet2.DoQuery("UPDATE INV1 set U_FreeDP=" & (LargeBP) & " where [DocEntry]=" & DocNum & " and LineNum=" & LineNum & "")
                    Catch ex As Exception

                    End Try
                    If LargeBP <> 0 Then
                        LargeBP = 0
                    End If
                End If

                oRecordSet.MoveNext()
            Next
            '---annual pack
            Edt = Format(Now.Date.AddMonths(12), "yyyyMMdd")
            For i = 1 To oRecordSet6.RecordCount
                Dim CardCode As String = oRecordSet6.Fields.Item(0).Value

                PetSize = PSize(oRecordSet6.Fields.Item(2).Value, ocompany)
                Dim PackType = oRecordSet6.Fields.Item(3).Value
                Dim oRecordSet7 As SAPbobsCOM.Recordset
                oRecordSet7 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet7.DoQuery("SELECT T0.[U_AI_DaysPerWeek] FROM [dbo].[@AI_CONTRACTTYPE]  T0 WHERE T0.[Code] ='" & PackType & "'")
                If PetSize = "Small" Then
                    Qty = oRecordSet6.Fields.Item(1).Value
                    If oRecordSet7.Fields.Item(0).Value = 7 Then
                        Dim year As Integer = Now.Date.Year
                        Dim Month As Integer = Now.Date.Month
                        Dim days As Integer = System.DateTime.DaysInMonth(year, Month)
                        Qty = Qty * days
                    Else
                        Qty = Qty * oRecordSet7.Fields.Item(0).Value * 4
                    End If

                    'oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + BonusPoint
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + BonusPoint
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewAccTrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'update ocrd set groupcode=" & GroupCode & ",
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                ElseIf PetSize = "Medium" Then

                    Qty = oRecordSet6.Fields.Item(1).Value
                    If oRecordSet7.Fields.Item(0).Value = 7 Then
                        Dim year As Integer = Now.Date.Year
                        Dim Month As Integer = Now.Date.Month
                        Dim days As Integer = System.DateTime.DaysInMonth(year, Month)
                        Qty = Qty * days
                    Else
                        Qty = Qty * oRecordSet7.Fields.Item(0).Value * 4
                    End If
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassM], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedM] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    'oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + BonusPoint
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + BonusPoint
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewAccTrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                ElseIf PetSize = "Large" Then
                    Qty = oRecordSet6.Fields.Item(1).Value
                    If oRecordSet7.Fields.Item(0).Value = 7 Then
                        Dim year As Integer = Now.Date.Year
                        Dim Month As Integer = Now.Date.Month
                        Dim days As Integer = System.DateTime.DaysInMonth(year, Month)
                        Qty = Qty * days
                    Else
                        Qty = Qty * oRecordSet7.Fields.Item(0).Value * 4
                    End If
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassB], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedB] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + BonusPoint
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + BonusPoint
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewAccTrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "' , groupcode=" & GroupCode & ",U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                End If

                oRecordSet6.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            oRecordSet6 = Nothing
            GC.Collect()
        Catch ex As Exception
            ' SBO_Application.MessageBox(ex.Message)
        End Try
    End Sub
#End Region

    Private Sub SBO_Application_FormDataEvent(ByRef BusinessObjectInfo As SAPbouiCOM.BusinessObjectInfo, ByRef BubbleEvent As Boolean) Handles SBO_Application.FormDataEvent

    End Sub
    Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent
        Try
            '********* CN******************************
            If pVal.FormType = 179 And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And pVal.ItemUID = "1" And pVal.Before_Action = True Then
                oForm = SBO_Application.Forms.Item(FormUID)
                oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet.DoQuery("SELECT Max(cast(DocEntry as int))  FROM ORIN")
                DocEtry = oRecordSet.Fields.Item(0).Value
                'SBO_Application.MessageBox(DocEtry)
            End If
            If pVal.FormType = 179 And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And pVal.ItemUID = "1" And pVal.Before_Action = False Then

                oForm = SBO_Application.Forms.Item(FormUID)

                Dim DocEntrySBO As Integer
                Dim oRecordSetINV As SAPbobsCOM.Recordset
                oRecordSetINV = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSetINV.DoQuery("SELECT Max(cast(DocEntry as int))  FROM ORIN")
                DocEntrySBO = oRecordSetINV.Fields.Item(0).Value
                ' SBO_Application.MessageBox(DocEntrySBO)

                If DocEntrySBO = (DocEtry + 1) Then
                    'SBO_Application.MessageBox(DocEntrySBO)
                    UpdateTransport_CN(DocEntrySBO.ToString)
                    UpdateBoarding_CN(DocEntrySBO.ToString)
                    UpdateDayPass_CN(DocEntrySBO.ToString)
                End If


            End If
            'gopi
            '***********CN ****************************
            If pVal.FormType = 133 And pVal.EventType = SAPbouiCOM.BoEventTypes.et_VALIDATE And pVal.ItemUID = "38" And (pVal.ColUID = "11") And pVal.Before_Action = False And pVal.InnerEvent = False Then
                oForm = SBO_Application.Forms.Item(FormUID)

                Dim itemCode As String = ""
                Dim Type As String = ""
                Dim PkgType As String = ""
                oMatrix = oForm.Items.Item("38").Specific
                oColumns = oMatrix.Columns
                oEdit = oMatrix.Columns.Item("1").Cells.Item(pVal.Row).Specific
                itemCode = oEdit.String
                oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet.DoQuery("SELECT T0.[ItemCode], T0.[ItemName] FROM OITM T0 WHERE (T0.[ItmsGrpCod] ='102') and  T0.[ItemCode] ='" & itemCode & "'")
                Try
                    oCombo = oMatrix.Columns.Item("U_AI_PackageType").Cells.Item(pVal.Row).Specific
                    PkgType = oCombo.Selected.Value
                Catch ex As Exception

                End Try

                If oRecordSet.RecordCount > 0 Then
                    Type = "AP"
                End If
                oRecordSet.DoQuery("SELECT T0.[ItemCode], T0.[ItemName] FROM OITM T0 WHERE (T0.[ItmsGrpCod] ='104') and  T0.[ItemCode] ='" & itemCode & "'")
                If oRecordSet.RecordCount > 0 Then
                    Type = "DP"
                End If
                If Type = "" Then
                    Exit Sub
                End If
                oEdit = oForm.Items.Item("4").Specific
                oRecordSet.DoQuery("SELECT T0.[U_AI_ACCDAYPASS] FROM OCRD T0 WHERE T0.[CardCode] =" & oEdit.String & "")
                Dim NewAccTrans As Integer = oRecordSet.Fields.Item(0).Value
                Dim i As Integer

                Dim Qty As Integer = 0
                For i = 1 To oMatrix.RowCount
                    oEdit = oMatrix.Columns.Item("1").Cells.Item(i).Specific
                    oRecordSet.DoQuery("SELECT T0.[ItemCode], T0.[ItemName] FROM OITM T0 WHERE (T0.[ItmsGrpCod] ='102' or  T0.[ItmsGrpCod] ='104') and  T0.[ItemCode] ='" & oEdit.String & "'")
                    If oRecordSet.RecordCount > 0 Then
                        oEdit = oMatrix.Columns.Item("11").Cells.Item(i).Specific
                        Qty = oEdit.Value
                        Exit For
                    End If
                Next
                NewAccTrans = NewAccTrans + Qty
                Dim GroupCode As String = ""
                If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                    GroupCode = "@Non-Member"
                ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                    GroupCode = "Silver"
                ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                    GroupCode = "Gold"
                ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                    GroupCode = "Platinum"
                ElseIf NewAccTrans >= 320 Then
                    GroupCode = "Royal Platinum"
                End If
                oEdit = oMatrix.Columns.Item("U_AI_PetCode").Cells.Item(pVal.Row).Specific
                oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet.DoQuery("SELECT distinct T0.[U_AI_Size] FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_PetCode] ='" & oEdit.String & "'")
                Dim Size As String = ""
                Size = oRecordSet.Fields.Item(0).Value

                Dim UP As Double = 0
                If Size = "Small" And Type = "DP" Then
                    oRecordSet.DoQuery("SELECT T0.[U_AI_DPPSmall] FROM [dbo].[@AI_PMATRIXDP]  T0 WHERE T0.[U_AI_MemberType] = '" & GroupCode & "'")
                    UP = oRecordSet.Fields.Item(0).Value
                ElseIf Size = "Medium" And Type = "DP" Then
                    oRecordSet.DoQuery("SELECT T0.[U_AI_DPPMedium] FROM [dbo].[@AI_PMATRIXDP]  T0 WHERE T0.[U_AI_MemberType] = '" & GroupCode & "'")
                    UP = oRecordSet.Fields.Item(0).Value
                ElseIf Size = "Large" And Type = "DP" Then
                    oRecordSet.DoQuery("SELECT T0.[U_AI_DPPBig] FROM [dbo].[@AI_PMATRIXDP]  T0 WHERE T0.[U_AI_MemberType] = '" & GroupCode & "'")
                    UP = oRecordSet.Fields.Item(0).Value
                ElseIf Size = "Small" And Type = "AP" Then
                    oRecordSet.DoQuery("SELECT T0.[U_AI_MPPSmall] FROM [dbo].[@AI_PMATRIXANP]  T0 WHERE T0.[U_AI_PackageType] ='" & PkgType & "'")
                    UP = oRecordSet.Fields.Item(0).Value
                ElseIf Size = "Medium" And Type = "AP" Then
                    oRecordSet.DoQuery("SELECT T0.[U_AI_MPPMedium] FROM [dbo].[@AI_PMATRIXANP]  T0 WHERE T0.[U_AI_PackageType] ='" & PkgType & "'")
                    UP = oRecordSet.Fields.Item(0).Value
                ElseIf Size = "Large" And Type = "AP" Then
                    oRecordSet.DoQuery("SELECT T0.[U_AI_MPPBig] FROM [dbo].[@AI_PMATRIXANP]  T0 WHERE T0.[U_AI_PackageType] ='" & PkgType & "'")
                    UP = oRecordSet.Fields.Item(0).Value
                End If
                If UP <> 0.0 Then
                    oEdit = oMatrix.Columns.Item("14").Cells.Item(pVal.Row).Specific
                    oEdit.Value = UP
                End If
            End If
            '-------------Sales Order----------------
            If pVal.FormType = 139 And pVal.EventType = SAPbouiCOM.BoEventTypes.et_VALIDATE And pVal.ItemUID = "38" And (pVal.ColUID = "11") And pVal.Before_Action = False And pVal.InnerEvent = False Then
                oForm = SBO_Application.Forms.Item(FormUID)

                Dim itemCode As String = ""
                Dim Type As String = ""
                Dim PkgType As String = ""
                oMatrix = oForm.Items.Item("38").Specific
                oColumns = oMatrix.Columns
                oEdit = oMatrix.Columns.Item("1").Cells.Item(pVal.Row).Specific
                itemCode = oEdit.String
                oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet.DoQuery("SELECT T0.[ItemCode], T0.[ItemName] FROM OITM T0 WHERE (T0.[ItmsGrpCod] ='102') and  T0.[ItemCode] ='" & itemCode & "'")
                Try
                    oCombo = oMatrix.Columns.Item("U_AI_PackageType").Cells.Item(pVal.Row).Specific
                    PkgType = oCombo.Selected.Value
                Catch ex As Exception

                End Try

                If oRecordSet.RecordCount > 0 Then
                    Type = "AP"
                End If
                oRecordSet.DoQuery("SELECT T0.[ItemCode], T0.[ItemName] FROM OITM T0 WHERE (T0.[ItmsGrpCod] ='104') and  T0.[ItemCode] ='" & itemCode & "'")
                If oRecordSet.RecordCount > 0 Then
                    Type = "DP"
                End If
                If Type = "" Then
                    Exit Sub
                End If
                oEdit = oForm.Items.Item("4").Specific
                oRecordSet.DoQuery("SELECT T0.[U_AI_ACCDAYPASS] FROM OCRD T0 WHERE T0.[CardCode] =" & oEdit.String & "")
                Dim NewAccTrans As Integer = oRecordSet.Fields.Item(0).Value
                Dim i As Integer

                Dim Qty As Integer = 0
                For i = 1 To oMatrix.RowCount
                    oEdit = oMatrix.Columns.Item("1").Cells.Item(i).Specific
                    oRecordSet.DoQuery("SELECT T0.[ItemCode], T0.[ItemName] FROM OITM T0 WHERE (T0.[ItmsGrpCod] ='102' or  T0.[ItmsGrpCod] ='104') and  T0.[ItemCode] ='" & oEdit.String & "'")
                    If oRecordSet.RecordCount > 0 Then
                        oEdit = oMatrix.Columns.Item("11").Cells.Item(i).Specific
                        Qty = oEdit.Value
                        Exit For
                    End If
                Next
                NewAccTrans = NewAccTrans + Qty
                Dim GroupCode As String = ""
                If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                    GroupCode = "@Non-Member"
                ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                    GroupCode = "Silver"
                ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                    GroupCode = "Gold"
                ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                    GroupCode = "Platinum"
                ElseIf NewAccTrans >= 320 Then
                    GroupCode = "Royal Platinum"
                End If
                oEdit = oMatrix.Columns.Item("U_AI_PetCode").Cells.Item(pVal.Row).Specific
                oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet.DoQuery("SELECT distinct T0.[U_AI_Size] FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_PetCode] ='" & oEdit.String & "'")
                Dim Size As String = ""
                Size = oRecordSet.Fields.Item(0).Value

                Dim UP As Double = 0
                If Size = "Small" And Type = "DP" Then
                    oRecordSet.DoQuery("SELECT T0.[U_AI_DPPSmall] FROM [dbo].[@AI_PMATRIXDP]  T0 WHERE T0.[U_AI_MemberType] = '" & GroupCode & "'")
                    UP = oRecordSet.Fields.Item(0).Value
                ElseIf Size = "Medium" And Type = "DP" Then
                    oRecordSet.DoQuery("SELECT T0.[U_AI_DPPMedium] FROM [dbo].[@AI_PMATRIXDP]  T0 WHERE T0.[U_AI_MemberType] = '" & GroupCode & "'")
                    UP = oRecordSet.Fields.Item(0).Value
                ElseIf Size = "Large" And Type = "DP" Then
                    oRecordSet.DoQuery("SELECT T0.[U_AI_DPPBig] FROM [dbo].[@AI_PMATRIXDP]  T0 WHERE T0.[U_AI_MemberType] = '" & GroupCode & "'")
                    UP = oRecordSet.Fields.Item(0).Value
                ElseIf Size = "Small" And Type = "AP" Then
                    oRecordSet.DoQuery("SELECT T0.[U_AI_MPPSmall] FROM [dbo].[@AI_PMATRIXANP]  T0 WHERE T0.[U_AI_PackageType] ='" & PkgType & "'")
                    UP = oRecordSet.Fields.Item(0).Value
                ElseIf Size = "Medium" And Type = "AP" Then
                    oRecordSet.DoQuery("SELECT T0.[U_AI_MPPMedium] FROM [dbo].[@AI_PMATRIXANP]  T0 WHERE T0.[U_AI_PackageType] ='" & PkgType & "'")
                    UP = oRecordSet.Fields.Item(0).Value
                ElseIf Size = "Large" And Type = "AP" Then
                    oRecordSet.DoQuery("SELECT T0.[U_AI_MPPBig] FROM [dbo].[@AI_PMATRIXANP]  T0 WHERE T0.[U_AI_PackageType] ='" & PkgType & "'")
                    UP = oRecordSet.Fields.Item(0).Value
                End If
                If UP <> 0.0 Then
                    oEdit = oMatrix.Columns.Item("14").Cells.Item(pVal.Row).Specific
                    oEdit.Value = UP
                End If
            End If
            If pVal.FormType = 133 And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And pVal.ItemUID = "1" And pVal.Before_Action = True Then
                oForm = SBO_Application.Forms.Item(FormUID)
                oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet.DoQuery("SELECT Max(cast(DocEntry as int))  FROM OINV")
                DocEtry = oRecordSet.Fields.Item(0).Value
            End If
            If pVal.FormType = 133 And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And pVal.ItemUID = "1" And pVal.Before_Action = False Then

                oForm = SBO_Application.Forms.Item(FormUID)
                'Dim DtSet1 As System.Data.DataSet
                'DtSet1 = New System.Data.DataSet
                'Dim sPath As String
                'sPath = IO.Directory.GetParent(Application.StartupPath).ToString
                'Dim file As System.IO.StreamReader = New System.IO.StreamReader(sPath & "\" & "Pwd.txt", True)
                'Dim pwd As String
                'pwd = file.ReadLine()

                'Dim connstr As String = "Data Source=" & ocompany.Server & ";Initial Catalog=SBO-COMMON;User ID=sa;Password=" & pwd & ""
                'Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection(connstr)
                'conn.Open()
                'Try
                '    file.Close()
                'Catch ex As Exception

                'End Try

                'Dim sqlcmd As SqlClient.SqlCommand
                'Dim da As SqlClient.SqlDataAdapter
                ' oRecordSet.DoQuery("SELECT Max(cast(DocEntry as int))  FROM OINV")
                ' Dim str As String = "select Max(cast(FieldValue as int)) from sevt where FieldNames='DocEntry' and ObjectType='13' and SourceDB='" & ocompany.CompanyDB & "'"
                ' Dim str As String = "SELECT Max(cast(DocEntry as int))  FROM OINV"
                'sqlcmd = New SqlClient.SqlCommand(Str, conn)
                'da = New SqlClient.SqlDataAdapter(sqlcmd)
                'DtSet1.Clear()
                'da.Fill(DtSet1)
                Dim DocEntrySBO As Integer
                Dim oRecordSetINV As SAPbobsCOM.Recordset
                oRecordSetINV = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSetINV.DoQuery("SELECT Max(cast(DocEntry as int))  FROM OINV")

                DocEntrySBO = oRecordSetINV.Fields.Item(0).Value


                If DocEntrySBO = (DocEtry + 1) Then
                    'SBO_Application.MessageBox(DocEntrySBO)
                    UpdateTransport(DocEntrySBO.ToString)
                    UpdateBoarding(DocEntrySBO.ToString)
                    UpdateDayPass(DocEntrySBO.ToString)
                End If


            End If
            If pVal.FormType = 134 And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And pVal.ItemUID = "1" And pVal.Before_Action = True And pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Then
                oForm = SBO_Application.Forms.Item(FormUID)
                oEdit = oForm.Items.Item("5").Specific
                Dim BPCode As String = oEdit.String
                oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet1.DoQuery("SELECT T0.[CardCode], T0.[CardName], T0.[Address], T0.[ZipCode], T0.[Phone1], T0.[Phone2], T0.[Cellular], T0.[E_Mail] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
                oRecordSet1.MoveLast()
                OCCode = oRecordSet1.Fields.Item(0).Value
                OCName = oRecordSet1.Fields.Item(1).Value
                OBiiSt = oRecordSet1.Fields.Item(2).Value
                OZIp = oRecordSet1.Fields.Item(3).Value
                OPh1 = oRecordSet1.Fields.Item(4).Value
                OPh2 = oRecordSet1.Fields.Item(5).Value
                OCell = oRecordSet1.Fields.Item(6).Value
                OEmail = oRecordSet1.Fields.Item(7).Value
            End If

            If pVal.FormType = 134 And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And pVal.ItemUID = "1" And pVal.Before_Action = False And (pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Or pVal.FormMode = SAPbouiCOM.BoFormMode.fm_OK_MODE) Then
                Try
                    If OCCode = "" Then
                        Exit Try
                    End If

                    oForm = SBO_Application.Forms.Item(FormUID)
                    oEdit = oForm.Items.Item("5").Specific
                    Dim BPCode As String = oEdit.String
                    oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecordSet1.DoQuery("SELECT T0.[CardCode], T0.[CardName], T0.[Address], T0.[ZipCode], T0.[Phone1], T0.[Phone2], T0.[Cellular], T0.[E_Mail] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
                    oRecordSet1.MoveLast()
                    NCCode = oRecordSet1.Fields.Item(0).Value
                    NCName = oRecordSet1.Fields.Item(1).Value
                    NBiiSt = oRecordSet1.Fields.Item(2).Value
                    NZIp = oRecordSet1.Fields.Item(3).Value
                    NPh1 = oRecordSet1.Fields.Item(4).Value
                    NPh2 = oRecordSet1.Fields.Item(5).Value
                    NCell = oRecordSet1.Fields.Item(6).Value
                    NEmail = oRecordSet1.Fields.Item(7).Value

                    If OCName <> NCName Or NBiiSt <> OBiiSt Or NZIp <> OZIp Or NPh1 <> OPh1 Or NCell <> OCell Or NEmail <> OEmail Then
                        '--------------------
                        If SBO_Application.MessageBox("BP Master Get Updated.Would you Like to Send Email to Customer?", 1, "Yes", "No") = 1 Then
                            Try

                                Dim sPath As String
                                sPath = IO.Directory.GetParent(Application.StartupPath).ToString
                                Dim file As System.IO.StreamReader = New System.IO.StreamReader(sPath & "\K9\" & "Email.txt", True)
                                Dim Email As String
                                Email = file.ReadLine()
                                file.Close()
                                Dim MyArr As Array
                                MyArr = Email.Split(";")
                                'Email
                                Dim SmtpServer As New SmtpClient()
                                SmtpServer.Credentials = New Net.NetworkCredential(MyArr(0).ToString, MyArr(1).ToString)
                                SmtpServer.Port = 587
                                SmtpServer.Host = MyArr(2).ToString
                                SmtpServer.EnableSsl = True
                                Dim mail As New MailMessage()
                                mail = New MailMessage()
                                'Dim addr() As String = oRecordSet2.Fields.Item(0).Value.ToString().Split(",")
                                Try
                                    mail.From = New MailAddress(MyArr(0).ToString, "K9 Kulture Lifestyle Pte Ltd", System.Text.Encoding.UTF8)
                                    mail.To.Add(NEmail)
                                    mail.Subject = "Customer Information"
                                    mail.Body = "Dear  " & NCName & " your New Contact Deatils Address : " & NBiiSt & " Postal Code : " & NZIp & " Phone1 : " & NPh1 & " Phone2 : " & NPh2 & " Mobile : " & NCell & " and Email : " & NEmail & ""
                                    mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
                                    mail.ReplyTo = New MailAddress(MyArr(0).ToString)
                                    SmtpServer.Send(mail)
                                Catch ex As Exception
                                    SBO_Application.MessageBox(ex.ToString())
                                End Try
                            Catch ex As Exception

                            End Try
                        End If


                    End If
                Catch ex As Exception

                End Try
            End If
            '    Try


            '        Dim oForm As SAPbouiCOM.Form

            '        Dim oItem As SAPbouiCOM.Item

            '        oForm = SBO_Application.Forms.Item(FormUID)

            '        oItem = oForm.Items.Item("29")

            '        oItem.Visible = False

            '        oItem = oForm.Items.Item("22")

            '        oItem.Visible = False
            '        oItem = oForm.Items.Item("42")

            '        oItem.Visible = False
            '        oItem = oForm.Items.Item("89")

            '        oItem.Visible = False
            '        oItem = oForm.Items.Item("103")

            '        oItem.Visible = False
            '        oItem = oForm.Items.Item("27")

            'Dim oColumn As SAPbouiCOM.Column
            'oMatrix = oForm.Items.Item("38").Specific
            'oColumns = oMatrix.Columns

            'oColumn = oColumns.Item("14")
            'oColumn.Visible = False
            'oColumn = oColumns.Item("15")
            'oColumn.Visible = False
            'oColumn = oColumns.Item("18")
            'oColumn.Visible = False
            '        oColumn = oColumns.Item("21")
            '        oColumn.Visible = False


            '        oItem.Visible = False
            '    Catch ex As Exception

            '        SBO_Application.MessageBox(ex.Message)

            '    End Try

            ' End If
            If pVal.FormType = 134 And pVal.ItemUID = "1" And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And pVal.Before_Action = False Then
                oOrderForm = SBO_Application.Forms.GetFormByTypeAndCount(pVal.FormType, pVal.FormTypeCount)

            End If
            If pVal.FormType = 134 And pVal.ItemUID = "SPEC" And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED And pVal.Before_Action = False Then
                oOrderForm = SBO_Application.Forms.GetFormByTypeAndCount(pVal.FormType, pVal.FormTypeCount)
                oOrderForm.PaneLevel = 12
            End If
            If ((pVal.FormType = 134 And pVal.EventType <> SAPbouiCOM.BoEventTypes.et_FORM_UNLOAD) And (pVal.Before_Action = True)) Then
                oOrderForm = SBO_Application.Forms.GetFormByTypeAndCount(pVal.FormType, pVal.FormTypeCount)
                If ((pVal.EventType = SAPbouiCOM.BoEventTypes.et_FORM_LOAD) And (pVal.Before_Action = True)) Then
                    Try
                        Try
                            'Lead()
                        Catch ex As Exception

                        End Try

                        oForm = SBO_Application.Forms.GetFormByTypeAndCount(pVal.FormType, pVal.FormTypeCount)
                        oNewItem = oOrderForm.Items.Add("SPEC", SAPbouiCOM.BoFormItemTypes.it_FOLDER)
                        oItem = oOrderForm.Items.Item("9")
                        oNewItem.Top = oItem.Top
                        oNewItem.Height = oItem.Height
                        oNewItem.Width = oItem.Width
                        oNewItem.Left = oItem.Left + oItem.Width
                        oFolderItem = oNewItem.Specific
                        oFolderItem.Caption = "Special Instruction"
                        oFolderItem.GroupWith("9")
                        oFolderItem.Pane = 12

                        '===============================================
                        oItem = oOrderForm.Items.Item("43")
                        oNewItem = oOrderForm.Items.Add("e1", SAPbouiCOM.BoFormItemTypes.it_COMBO_BOX)
                        oNewItem.Left = oItem.Left + 90
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oCombo = oNewItem.Specific
                        oCombo.DataBind.SetBound(True, "OCRD", "U_AI_Contract")
                        ComboLoad_ContractType(oOrderForm, oCombo)
                        oItem = oOrderForm.Items.Item("44")
                        oNewItem = oOrderForm.Items.Add("s1", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width + 85
                        oNewItem.Top = oItem.Top
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "e1"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Contract Type"
                        '  Exit Sub
                        '===============================================
                        oItem = oOrderForm.Items.Item("e1")
                        oNewItem = oOrderForm.Items.Add("ee2", SAPbouiCOM.BoFormItemTypes.it_EDIT)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oEdit = oNewItem.Specific
                        oEdit.DataBind.SetBound(True, "OCRD", "U_AI_NoDayPass")
                        oItem = oOrderForm.Items.Item("s1")
                        oNewItem = oOrderForm.Items.Add("ss2", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "ee2"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Last Purchase Day Passes"
                        '===============================================
                        oItem = oOrderForm.Items.Item("ee2")
                        oNewItem = oOrderForm.Items.Add("e2", SAPbouiCOM.BoFormItemTypes.it_EDIT)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oEdit = oNewItem.Specific
                        oEdit.DataBind.SetBound(True, "OCRD", "U_AI_DayPassUsed")
                        oItem = oOrderForm.Items.Item("ss2")
                        oNewItem = oOrderForm.Items.Add("s2", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "e2"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Day Pass Used as of Last Visit Date"
                        '===============================================

                        oItem = oOrderForm.Items.Item("e2")
                        oNewItem = oOrderForm.Items.Add("e3", SAPbouiCOM.BoFormItemTypes.it_EDIT)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oEdit = oNewItem.Specific
                        oEdit.DataBind.SetBound(True, "OCRD", "U_AI_DayPassBal")
                        oItem = oOrderForm.Items.Item("s2")
                        oNewItem = oOrderForm.Items.Add("s3", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "e3"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Day Pass Balance as of Last Visit Date"
                        '===============================================

                        oItem = oOrderForm.Items.Item("e3")
                        oNewItem = oOrderForm.Items.Add("e4", SAPbouiCOM.BoFormItemTypes.it_EDIT)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oEdit = oNewItem.Specific
                        oEdit.DataBind.SetBound(True, "OCRD", "U_AI_LastVisit")
                        oItem = oOrderForm.Items.Item("s3")
                        oNewItem = oOrderForm.Items.Add("s4", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "e4"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Last Visit Date"
                        '===============================================
                        oItem = oOrderForm.Items.Item("e4")
                        oNewItem = oOrderForm.Items.Add("e5", SAPbouiCOM.BoFormItemTypes.it_EDIT)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oEdit = oNewItem.Specific
                        oEdit.DataBind.SetBound(True, "OCRD", "U_AI_NoOfPet")
                        oItem = oOrderForm.Items.Item("s4")
                        oNewItem = oOrderForm.Items.Add("s5", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "e5"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Number of Pet"
                        '===============================================

                        oItem = oOrderForm.Items.Item("e5")
                        oNewItem = oOrderForm.Items.Add("e6", SAPbouiCOM.BoFormItemTypes.it_EDIT)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oEdit = oNewItem.Specific
                        oEdit.DataBind.SetBound(True, "OCRD", "U_AI_PetDetail")
                        oItem = oOrderForm.Items.Item("s5")
                        oNewItem = oOrderForm.Items.Add("s6", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "e6"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Pet Details"
                        '===============================================
                        oItem = oOrderForm.Items.Item("e6")
                        oNewItem = oOrderForm.Items.Add("e7", SAPbouiCOM.BoFormItemTypes.it_EDIT)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oEdit = oNewItem.Specific
                        oEdit.DataBind.SetBound(True, "OCRD", "U_AI_HowKnowK9")
                        oItem = oOrderForm.Items.Item("s6")
                        oNewItem = oOrderForm.Items.Add("s7", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "e7"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Knowledge About K9"
                        '===============================================
                        oItem = oOrderForm.Items.Item("e7")
                        oNewItem = oOrderForm.Items.Add("e8", SAPbouiCOM.BoFormItemTypes.it_EDIT)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oEdit = oNewItem.Specific
                        oEdit.DataBind.SetBound(True, "OCRD", "U_AI_ApptDate")
                        oItem = oOrderForm.Items.Item("s7")
                        oNewItem = oOrderForm.Items.Add("s8", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "e8"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Initial Appointment Date Selected by Customer"
                        '===============================================
                        oItem = oOrderForm.Items.Item("e8")
                        oNewItem = oOrderForm.Items.Add("e9", SAPbouiCOM.BoFormItemTypes.it_EDIT)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oEdit = oNewItem.Specific
                        oEdit.DataBind.SetBound(True, "OCRD", "U_AI_ApptTime")
                        oItem = oOrderForm.Items.Item("s8")
                        oNewItem = oOrderForm.Items.Add("s9", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "e9"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Initial Appt. Time"

                        '===============================================
                        oItem = oOrderForm.Items.Item("e9")
                        oNewItem = oOrderForm.Items.Add("e10", SAPbouiCOM.BoFormItemTypes.it_EDIT)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oEdit = oNewItem.Specific
                        oEdit.DataBind.SetBound(True, "OCRD", "U_AI_ACCDAYPASS")
                        oItem = oOrderForm.Items.Item("s9")
                        oNewItem = oOrderForm.Items.Add("s10", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "e10"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Accumulative No of Day Passes"
                        '===============================================
                        oItem = oOrderForm.Items.Item("e10")
                        oNewItem = oOrderForm.Items.Add("e11", SAPbouiCOM.BoFormItemTypes.it_EDIT)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oEdit = oNewItem.Specific
                        oEdit.DataBind.SetBound(True, "OCRD", "U_AI_TOTDP")
                        oItem = oOrderForm.Items.Item("s10")
                        oNewItem = oOrderForm.Items.Add("s11", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "e11"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Total No of Day Passes Usage"
                        '===============================================
                        oItem = oOrderForm.Items.Item("e11")
                        oNewItem = oOrderForm.Items.Add("e12", SAPbouiCOM.BoFormItemTypes.it_EDIT)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oEdit = oNewItem.Specific
                        oEdit.DataBind.SetBound(True, "OCRD", "U_AI_ExpDate")
                        oItem = oOrderForm.Items.Item("s11")
                        oNewItem = oOrderForm.Items.Add("s12", SAPbouiCOM.BoFormItemTypes.it_STATIC)
                        oNewItem.Left = oItem.Left
                        oNewItem.Width = oItem.Width
                        oNewItem.Top = oItem.Top + 15
                        oNewItem.Height = 14
                        oNewItem.FromPane = 12
                        oNewItem.ToPane = 12
                        oNewItem.LinkTo = "e12"
                        oStatic = oNewItem.Specific
                        oStatic.Caption = "Expiry date"

                    Catch ex As Exception
                    End Try
                    ''Try
                    ''    oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    ''    oRecordSet.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T0.[DocNum],T0.[TaxDate] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE T1.[ItemCode] ='Day Passes' and  T0.[U_Status] ='No'")
                    ''    Dim i As Integer = 0
                    ''    For i = 0 To oRecordSet.RecordCount

                    ''        oRecordSet3 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    ''        oRecordSet3.DoQuery("select U_AI_ACCDAYPASS from ocrd where cardcode= '" & oRecordSet.Fields.Item(0).Value & "'")
                    ''        Dim newtot As Integer = oRecordSet.Fields.Item(1).Value + oRecordSet3.Fields.Item(0).Value
                    ''        Dim oBP As SAPbobsCOM.BusinessPartners
                    ''        oBP = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oBusinessPartners)
                    ''        Dim SerrorMsg As String = ""
                    ''        Dim GroupCode As Integer


                    ''        If newtot >= 20 And newtot <= 39 Then

                    ''            GroupCode = 100

                    ''        ElseIf newtot >= 40 And newtot <= 79 Then
                    ''            GroupCode = 102

                    ''        ElseIf newtot >= 80 And newtot <= 319 Then
                    ''            GroupCode = 103

                    ''        ElseIf newtot >= 320 Then

                    ''            GroupCode = 104

                    ''        End If
                    ''        Dim exdt As Date = oRecordSet.Fields.Item(3).Value
                    ''        Dim exdt1 As String = Format(exdt.AddMonths(6), "yyyyMMdd")
                    ''        oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    ''        Dim st As String = "update ocrd set groupcode=" & GroupCode & ", U_AI_NoDayPass ='" & oRecordSet.Fields.Item(1).Value & "',U_AI_ACCDAYPASS=" & newtot & ",U_AI_ExpDate='" & exdt1 & "' where cardcode= '" & oRecordSet.Fields.Item(0).Value & "'"
                    ''        oRecordSet1.DoQuery(st)
                    ''        oRecordSet2 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    ''        oRecordSet2.DoQuery("update oinv set U_Status ='Yes' where DocNum= '" & oRecordSet.Fields.Item(2).Value & "'")
                    ''        oRecordSet.MoveNext()
                    ''    Next

                    ''           Catch ex As Exception

                    '' End Try
                    ''Try
                    ''    Dim dt As String = Format(Now.Date, "yyyyMMdd")

                    ''    oRecordSet2 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    ''    oRecordSet2.DoQuery("SELECT T0.[CardCode] FROM OCRD T0 WHERE T0.[U_AI_ExpDate] ='" & dt & "'")
                    ''    oRecordSet2.MoveFirst()
                    ''    Dim i As Integer = 0
                    ''    For i = 0 To oRecordSet2.RecordCount - 1
                    ''        oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    ''        Dim cn As String = oRecordSet2.Fields.Item(0).Value
                    ''        Dim st As String = "update ocrd set U_AI_NoDayPass=0 where cardcode= '" & oRecordSet2.Fields.Item(0).Value & "'"
                    ''        oRecordSet1.DoQuery(st)
                    ''        oRecordSet2.MoveNext()
                    ''    Next

                    ''Catch ex As Exception

                    ''End Try

                End If

            End If

        Catch ex As Exception
            ' SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Private Sub ComboLoad_ContractType(ByRef Oform As SAPbouiCOM.Form, ByRef oCombo As SAPbouiCOM.ComboBox)
        Try
            oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.Code, T0.Name FROM [dbo].[@AI_CONTRACTTYPE]  T0")
            Dim it As Integer = 1
            For it = oCombo.ValidValues.Count - 1 To 0 Step -1
                oCombo.ValidValues.Remove(it, SAPbouiCOM.BoSearchKey.psk_Index)
            Next
            Do While Not oRecordSet.EoF
                oCombo.ValidValues.Add(oRecordSet.Fields.Item(0).Value, oRecordSet.Fields.Item(1).Value)
                oRecordSet.MoveNext()
            Loop
            oRecordSet = Nothing
            GC.Collect()
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Private Sub SBO_Application_MenuEvent(ByRef pVal As SAPbouiCOM.MenuEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.MenuEvent
        Try
            If pVal.BeforeAction = True Then
                If pVal.MenuUID = "MD1" Then
                    LoadFromXML("PetMasterData.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_PETMASTER")
                    oPetMaster.PetMaster_bind(oForm)
                    ''Dim frm As Form1
                    ''frm = New Form1
                    ' ''frm.AE_MAWB_Report(sqlstr, sqlstr1, sqlstr2, ocompany)
                    ''frm.Text = "Air Export MAWB"
                    ''frm.TopMost = True
                    ''frm.Activate()
                    ''frm.ShowDialog()
                    'LoadFromXML("Samp.srf", SBO_Application)
                    'oForm = SBO_Application.Forms.Item("Samp")
                    ' oPetMaster.PetMaster_bind(oForm)

                ElseIf pVal.MenuUID = "MD2" Then
                    LoadFromXML("AppointmentMaster.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_APPOINTMENT")
                    oAppointmentMaster.AppointmentMaster_bind(oForm)
                ElseIf pVal.MenuUID = "MD3" Then
                    LoadFromXML("ProgramMaster.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_PROGMASTER")
                    oProgramMaster.ProgramMaster_bind(oForm)
                ElseIf pVal.MenuUID = "MD4" Then
                    LoadFromXML("RouteMaster_FrontEnd.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_ROUTEMASTER")
                    oRouteMaster.RouteMaster_bind(oForm)
                ElseIf pVal.MenuUID = "MD6" Then
                    LoadFromXML("Delete_RoutePlan.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AT_DELROUTEPLAN")
                    ' oRouteMaster.RouteMaster_bind(oForm)

                ElseIf pVal.MenuUID = "TR1" Then
                    'Grouping(ocompany)
                    LoadFromXML("Grouping.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_GROUPING")
                    oGrouping.Grouping_bind(oForm)
                ElseIf pVal.MenuUID = "TR2" Then
                    ' Activities(ocompany)
                    LoadFromXML("ACTIVITY.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_ACTIVITY")
                    oActivities.Activities_bind(oForm)
                ElseIf pVal.MenuUID = "TR3" Then
                    LoadFromXML("Attendance.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_ATTENDANCE")
                    oAttendance.Attendance_bind(oForm)
                ElseIf pVal.MenuUID = "TR4" Then
                    LoadFromXML("Incident Report1.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_INCIDENT")
                    oIncident.Incident_bind(oForm)
                ElseIf pVal.MenuUID = "TR5" Then
                    LoadFromXML("Food and Medicine.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_FOODMED")
                    oFood_Medicine.Food_bind(oForm)
                ElseIf pVal.MenuUID = "TR6" Then
                    LoadFromXML("ThickCheck.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_TICKS")
                    oAttendance.Attendance_Ticks_bind(oForm)
                ElseIf pVal.MenuUID = "TR8" Then
                    LoadFromXML("TrainingEvaluationTestform.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_Training")
                    oActivities.Activities_bind_TraingEvl(oForm)
                    'oIncident.Incident_bind(oForm)
                    'ElseIf pVal.MenuUID = "RE2" Then
                    '    LoadFromXML("Progress Sheet Report.srf", SBO_Application)
                    '    oForm = SBO_Application.Forms.Item("ProgressReport")
                    '    oActivities.Activities_bind_Prog(oForm)
                ElseIf pVal.MenuUID = "RE4" Then
                    LoadFromXML("Activities Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("ActivitiesReport")
                    oActivities.Activities_bind_Act(oForm)
                ElseIf pVal.MenuUID = "RE7" Then
                    LoadFromXML("Dog Inventory Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("DogInventoryReport")
                    oAttendance.Attendance_bind_DogInv(oForm)
                ElseIf pVal.MenuUID = "RE9" Then
                    LoadFromXML("Incident Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("IncidentReport")
                    oIncident.Incident_bind_Report(oForm)
                ElseIf pVal.MenuUID = "RE3" Then
                    LoadFromXML("Attendance Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AttendanceReport")
                    oAttendance.Attendance_bind_AttenReport(oForm)
                ElseIf pVal.MenuUID = "RE1" Then
                    LoadFromXML("Day Pass Balance Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("DayPassBalanceReport")
                    oAttendance.Attendance_bind_DayPassReport(oForm)
                ElseIf pVal.MenuUID = "RE2" Then
                    LoadFromXML("Evl Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("Evaluation_Report")
                    oPetMaster.Evaluation_Report_bind_Report(oForm)
                    'oAttendance.Attendance_bind_DayPassReport(oForm)
                ElseIf pVal.MenuUID = "RE8" Then
                    LoadFromXML("Food and Medicine Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("FoodMedicine")
                    oFood_Medicine.Food_bind_Report(oForm)
                ElseIf pVal.MenuUID = "RE6" Then
                    LoadFromXML("New Transport and New Boarding Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("NewTrans")
                    oRouteMaster.NewTrans_bind_Report(oForm)
                ElseIf pVal.MenuUID = "RE5" Then
                    LoadFromXML("Boarding Dogs Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("BoardingDogsReport")
                    oPetMaster.Boardind_bind_Report(oForm)
                ElseIf pVal.MenuUID = "TR7" Then
                    LoadFromXML("Bording.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_BORDING")
                    oBoarding.Boarding_bind(oForm)
                ElseIf pVal.MenuUID = "RE10" Then
                    LoadFromXML("Training_report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AI_TRI_REP")
                    oF_Training_Repors.Training_Report(oForm)

                ElseIf pVal.MenuUID = "RE11" Then
                    LoadFromXML("Count_Attendance_Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("Attendance_Summary_Report")
                    oR_Summary_Report.Bind_Common(oForm)

                ElseIf pVal.MenuUID = "RE12" Then
                    LoadFromXML("Count_Activities_Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("Activities_Summary_Report")
                    oR_Summary_Report.Bind_Common(oForm)

                ElseIf pVal.MenuUID = "RE13" Then
                    LoadFromXML("Count_Boarding_Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("Boarding_Summary_Report")
                    oR_Summary_Report.Bind_Common(oForm)

                ElseIf pVal.MenuUID = "RE14" Then
                    LoadFromXML("Count_Training_Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("Training_Summary_Report")
                    oR_Summary_Report.Bind_Training(oForm)
                ElseIf pVal.MenuUID = "RE15" Then
                    LoadFromXML("Last_Attendance Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("LastAttendanceReport")
                    oAttendance.LastAttendance_bind_AttenReport(oForm)

                ElseIf pVal.MenuUID = "RE17" Then
                    LoadFromXML("Annual Package Report.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("AnnualReport")
                    oAttendance.AnnualPackge_Report(oForm)

                ElseIf pVal.MenuUID = "TR9" Then
                    LoadFromXML("CreditMemo_SAP.srf", SBO_Application)
                    oForm = SBO_Application.Forms.Item("CreditNote")
                    oF_Refund.Refund_Bind(oForm)


                End If
            End If
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
        End Try
    End Sub
    Public Sub Lead()
        Try
            Dim oBP1 As SAPbobsCOM.BoObjectTypes
            oBP1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oBusinessPlaces)

            Dim oBP As SAPbobsCOM.BusinessPartners
            oBP = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oBusinessPartners)
            oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT [Cname],[Mno],[addr],[PetName],[breed],[gender],[age],[sno],[status] FROM AAALead where [status]='N'")
            Dim i As Integer = 0
            Dim return_value As Integer = 0
            For i = 0 To oRecordSet.RecordCount - 1
                Try
                    oBP.CardCode = "L0000" & oRecordSet.Fields.Item(7).Value
                    oBP.CardName = oRecordSet.Fields.Item(0).Value
                    oBP.CardType = SAPbobsCOM.BoCardTypes.cLid
                    oBP.GroupCode = 100
                    oBP.PayTermsGrpCode = -1
                    'oBP.gr = 100
                    oBP.Address = "L0000" & oRecordSet.Fields.Item(7).Value
                    oBP.BillToBuildingFloorRoom = oRecordSet.Fields.Item(2).Value

                    return_value = oBP.Add()
                    If return_value = 0 Then
                        oRecordSet1.DoQuery("update AAALead set status ='Y' where sno=" & oRecordSet.Fields.Item(7).Value & "")
                    End If
                    Dim SerrorMsg As String = ""
                    ocompany.GetLastError(return_value, SerrorMsg)
                    ' SBO_Application.MessageBox(ocompany.GetLastErrorDescription, ocompany.GetLastErrorCode)

                Catch ex As Exception

                End Try

            Next
        Catch ex As Exception

        End Try
    End Sub
    Public Sub Grouping(ByVal Ocompany1 As SAPbobsCOM.Company)
        Try
            oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT [SNo],[GroupCode],[PetCode] ,[ActCode],[GrpDate],[Trainer],[Status] FROM AAAGrouping where status='N'")
            If oRecordSet.RecordCount < 0 Then
                Exit Sub
            End If

            sCmp = ocompany.GetCompanyService
            'Get a handle to the SM_MOR UDO
            oGeneralService = sCmp.GetGeneralService("AI_GROUPING")
            'Specify data for main UDO
            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
            oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

            Dim i As Integer = 0
            Dim return_value As Integer = 0
            For i = 0 To oRecordSet.RecordCount - 1
                Try
                    Dim sno As Integer = oRecordSet.Fields.Item(0).Value
                    oGeneralData.SetProperty("U_Code", oRecordSet.Fields.Item(1).Value)
                    oGeneralData.SetProperty("U_AI_PetCode", oRecordSet.Fields.Item(2).Value)
                    oGeneralData.SetProperty("U_AI_ProgCode", oRecordSet.Fields.Item("ActCode").Value)
                    oGeneralData.SetProperty("U_AI_ProgName", ProgName(oRecordSet.Fields.Item("ActCode").Value, Ocompany1))
                    oGeneralData.SetProperty("U_AI_Date", oRecordSet.Fields.Item(4).Value)
                    oGeneralData.SetProperty("U_AI_Handler", oRecordSet.Fields.Item(5).Value)
                    oGeneralData.SetProperty("U_DocDate", Now.Date)
                    oGeneralData.SetProperty("U_Status", "Open")
                    Dim petname As String = PName(oRecordSet.Fields.Item(2).Value, Ocompany1)
                    oGeneralData.SetProperty("U_AI_PetName", petname)
                    oGeneralService.Add(oGeneralData)
                    'dim str as String=
                    oRecordSet1.DoQuery("update AAAGrouping set status ='Y' where sno=" & sno & "")
                Catch ex As Exception

                End Try
            Next

        Catch ex As Exception

        End Try
    End Sub
    Public Sub Activities(ByVal Ocompany1 As SAPbobsCOM.Company)
        Try
            oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT [sno],[ActCode],[PetCode],[ActDate],[TimeStrt],[TimeEnd],[Handler],[Status] FROM AAAActivity where Status='N'")
            If oRecordSet.RecordCount < 0 Then
                Exit Sub
            End If
            sCmp = ocompany.GetCompanyService
            oGeneralService = sCmp.GetGeneralService("AI_ACTIVITY")
            oGeneralData = oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralData)
            oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim i As Integer = 0
            Dim return_value As Integer = 0
            For i = 0 To oRecordSet.RecordCount - 1
                Try
                    Dim dt As Date = oRecordSet.Fields.Item(4).Value
                    Dim st As String = dt.TimeOfDay.Hours
                    Dim sno As Integer = oRecordSet.Fields.Item(0).Value
                    oGeneralData.SetProperty("U_AI_ProgCode", oRecordSet.Fields.Item(1).Value)
                    oGeneralData.SetProperty("U_AI_ProgName", ProgName(oRecordSet.Fields.Item("ActCode").Value, Ocompany1))
                    oGeneralData.SetProperty("U_AI_PetCode", oRecordSet.Fields.Item(2).Value)
                    oGeneralData.SetProperty("U_AI_PetName", PName(oRecordSet.Fields.Item(2).Value, Ocompany1))
                    oGeneralData.SetProperty("U_AI_Date", oRecordSet.Fields.Item("ActDate").Value)
                    'oGeneralData.SetProperty("U_AI_TimeStart", oRecordSet.Fields.Item(4).Value)
                    'oGeneralData.SetProperty("U_AI_TimeEnd", oRecordSet.Fields.Item(5).Value)
                    oGeneralData.SetProperty("U_AI_Status", "Ended")
                    oGeneralData.SetProperty("U_AI_Handler", oRecordSet.Fields.Item(6).Value)
                    oGeneralData.SetProperty("U_DocDate", Now.Date)
                    oGeneralData.SetProperty("U_Status", "Open")
                    oGeneralService.Add(oGeneralData)
                    'dim str as String=
                    oRecordSet1.DoQuery("update AAAActivity set status ='Y' where sno=" & sno & "")
                Catch ex As Exception

                End Try
            Next

        Catch ex As Exception

        End Try
    End Sub
    Public Sub DraftSO()
        Try
            Exit Sub
            Dim Tdt As String = Format(Now.Date, "dd.MM.yy")
            oRecordSet = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))")
            Dim dt As Date = oRecordSet.Fields.Item(0).Value
            Dim dt1 As String = Format(dt.Date, "dd.MM.yy")
            If Tdt = dt1 Then
                oRecordSet1 = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet1.DoQuery("SELECT T0.[U_AI_TransType], T0.[U_AI_CustCode],count(*)as 'Count' FROM [dbo].[@AI_BOARDTRANS]  T0 WHERE T0.[U_AI_Date]  between( SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))) and (SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))) GROUP BY T0.[U_AI_CustCode], T0.[U_AI_TransType]")
                Dim oODRF As SAPbobsCOM.Documents = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)
                oODRF.DocObjectCode = SAPbobsCOM.BoObjectTypes.oOrders
                Dim i As Integer = 0
                oRecordSet1.MoveFirst()
                For i = 0 To oRecordSet1.RecordCount - 1
                    Dim ccdoe As String = oRecordSet1.Fields.Item(0).Value
                    Dim Icode As String = oRecordSet1.Fields.Item(1).Value
                    Dim Qty As String = oRecordSet1.Fields.Item(2).Value
                    If ccdoe <> "" And Icode <> "" And Qty <> "" Then

                        oODRF.CardCode = Icode
                        oODRF.DocDate = Now.Date
                        oODRF.Lines.ItemCode = ccdoe
                        oODRF.Lines.Quantity = Qty
                        oODRF.Lines.Add()
                        Dim k As Integer
                        k = oODRF.Add()
                        Dim SerrorMsg As String = ""
                        ocompany.GetLastError(k, SerrorMsg)
                        If i <> 0 Then
                            SBO_Application.MessageBox(ocompany.GetLastErrorDescription, ocompany.GetLastErrorCode)
                            Exit Sub
                        End If
                    End If
                    oRecordSet1.MoveNext()
                Next
            End If


        Catch ex As Exception

        End Try
    End Sub

    Private Sub SBO_Application_PrintEvent(ByRef eventInfo As SAPbouiCOM.PrintEventInfo, ByRef BubbleEvent As Boolean) Handles SBO_Application.PrintEvent

    End Sub

    Private Sub SBO_Application_ServerInvokeCompletedEvent(ByRef b1iEventArgs As SAPbouiCOM.B1iEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ServerInvokeCompletedEvent

    End Sub
End Class
